package com.jcz.daoImpl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.jcz.dao.AuctionDao;
import com.jcz.dao.HibernateUtil;
import com.jcz.entity.Auction;

public class AuctionDaoImpl extends HibernateDaoSupport implements AuctionDao {
	/*private SessionFactory sf=null;  继承之后不需要自己创建工厂*/

	
/*	public SessionFactory getSf() {
		return sf;
	}

	public void setSf(SessionFactory sf) {
		this.sf = sf;
	}*/

	/*获取总数量*/
	public Long findCount() {
		Session session=null;
		Long count=null;
		try {
			/*session=sf.openSession();*/
			HibernateTemplate temp=this.getHibernateTemplate();
			count=(long) temp.find("select count(a) from Auction a").size();
			/*count=(Long)session.createQuery("select count(a) from Auction a").uniqueResult();*/
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
	}
	
	/*按id查找*/
	@Override
	public Auction findById(Integer id) {
		Session session=null;
		Auction auction=new Auction();
		
		try {
			session=HibernateUtil.currentSession();
			auction=(Auction)session.get(Auction.class, id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return auction;
	}
	
	/*动态查询加分页*/
	public List<Auction> findAll(Integer size,Integer index,Auction auction) {
		Session session=null;
		List<Auction> list=null;
		
		try {
			session=HibernateUtil.currentSession();
			Criteria criteria=session.createCriteria(Auction.class);
			/*判断查询条件*/
			if(auction!=null){
				if(auction.getC_cname()!=null){
					criteria.add(Restrictions.ilike("c_cname", "%"+auction.getC_cname()+"%"));
				}
				if(auction.getDescribe()!=null){
					criteria.add(Restrictions.ilike("describe", "%"+auction.getDescribe()+"%"));/*模糊查询*/
				}
				if(auction.getStarttime()!=null){
					criteria.add(Restrictions.ge("starttime", auction.getStartprice()));/*ge：大于*/
				}
				if(auction.getEndtime()!=null){
					criteria.add(Restrictions.le("endtime", auction.getEndtime()));				}				
				if(auction.getStartprice()!=null){
					criteria.add(Restrictions.eq("startprice", auction.getStartprice()));/*eq:等于*/
				}
				
			}
			/*分页查询*/
			criteria.setFirstResult((index-1)*size);
			criteria.setMaxResults(size);
			list=criteria.list();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}


	/*添加数据*/
	@Override
	public boolean addAuction(Auction auction) {
		Session session=null;
		Transaction tx=null;
		int i=0;
		try {
			session=HibernateUtil.currentSession();
			tx=session.beginTransaction();
			
			session.save(auction);
			
			tx.commit();
			
		} catch (Exception e) {
			tx.rollback();
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		return true;
		
	}

	/*更新*/
	public boolean updateAuction(Auction auction){
		Session session=null;
		Transaction tx=null;
		
		try {
			session=HibernateUtil.currentSession();
			tx=session.beginTransaction();
			
			session.update(auction);
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		return true;
	}
}
